package com.baize.cpdepus.service.lmp;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baize.cpdepus.common.OssService;
import com.baize.cpdepus.common.RedisCache;
import com.baize.cpdepus.common.UploadingOssService;
import com.baize.cpdepus.config.ConstantPropertiesUtils;
import com.baize.cpdepus.entity.Article;
import com.baize.cpdepus.mapper.ArticleMapper;
import com.baize.cpdepus.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private OssService ossService;
    @Autowired
    private RedisCache redisCache;

    //需要修改 不启用
    @Override
    public List<Article> getRandom() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add(i);
        }
        return articleMapper.selectRandom(list);
    }

    @Override
    public Article getID(Integer ID) {
        redisCache.setDbIndex(2);
        Article articleOne = redisCache.getCacheObject("articleId:" + ID);
        if (Objects.nonNull(articleOne)) return articleOne;
        Article article = articleMapper.selectID(ID);
        redisCache.setCacheObject("articleId:" + ID, article);
        return article;
    }


    @Override
    public void addOne(Article article, MultipartFile file) throws IOException {
        String url = UploadingOssService.addOss(file);  //oss返回文件上传的路径
        article.setPicture(url);
        articleMapper.insertOne(article);
    }

    //没写 后面需要讨论对于这个上架数据需要保存 保存多久
    @Override
    public void removeOne(Integer ID) {
//        ossService.deleteFile(articleMapper.selectID(ID).getPicture());
        articleMapper.deleteOne(ID);
    }

    @Override
    public List<Article> getNameAll(String name) {
        redisCache.setDbIndex(2);
        List<Article> cacheObject = redisCache.getCacheObject("article:" + name);
        if (Objects.nonNull(cacheObject)) return cacheObject;
        List<Article> articles = articleMapper.getName(name);
        redisCache.setCacheObject("article:" + name, articles);
        return articles;
    }

    @Override
    public List<Article> getCategoryAll(Integer categoryById) {
        redisCache.setDbIndex(2);
        List<Article> cacheObject = redisCache.getCacheObject("categoryById:" + categoryById);
        if (Objects.nonNull(cacheObject)) return cacheObject;
        List<Article> articles = articleMapper.selectCategoryByIdAll(categoryById);
        redisCache.setCacheObject("categoryById" + categoryById, articles);
        return articles;
    }
}
